package com.example.mqttclient.weather;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.example.mqttclient.utils.NotificationUtils;

public class NetworkChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm != null ? cm.getActiveNetworkInfo() : null;

        if (ni != null && ni.isConnected()) {
            // 这里用context创建WeatherFetcher，确保上下文有效
            new WeatherFetcher(context, new WeatherFetcher.WeatherCallback() {
                @Override
                public void onWeatherFetched(String temperature, String weatherText) {
                    showWeatherNotification(context, temperature, weatherText);
                }
            }).execute();
        }
    }

    private void showWeatherNotification(Context context, String temp, String text) {
        NotificationUtils.showWeatherNotification(context, temp, text);
    }
}